package com.gopay.common.cps.dao.bt.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.bt.CpsGainCalRecordDAO;
import com.gopay.common.domain.cps.CpsGainsCalRecord;
/**
 * 
 * @CpsGainCalRecordDAOImpl.java
 * @author mz
 * @2013-8-1 上午11:58:06  www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("cpsGainCalRecordDAO")
public class CpsGainCalRecordDAOImpl extends BaseDAOImpl<CpsGainsCalRecord, Serializable> implements CpsGainCalRecordDAO{

    /*@Override
    public void updateGainCalRecord(String startDate, String endDate, String stlNum) {
        StringBuilder hql = new StringBuilder();
        hql.append(" update CpsGainsCalRecord set settleOrderNum=")
           .append(stlNum)
           .append(" where  createdTs<= to_date('")
           .append(endDate)
           .append(" 235959','yyyy/MM/dd HH24:mi:ss') ")
           .append(" and createdTs>=to_date('")
           .append(startDate)
           .append(" 235959','yyyy/MM/dd HH24:mi:ss') ");
       updateByHql(hql.toString());
    }*/

    @Override
    public List<CpsGainsCalRecord> getGainCalRecordWithTime(String startDate, String endDate,String jobId) {
        StringBuilder hql = new StringBuilder();
        HashMap<String, Object> map = new HashMap<String, Object>();
        hql.append("from CpsGainsCalRecord")
           .append(" where  1= 1 "); 
           if(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)){
                hql.append(" and createdTs<= to_timestamp(:endDate")
                   //.append(endDate)
                   .append(",'yyyy/MM/dd HH24:mi:ss.ff') ")
                   .append(" and createdTs>=to_timestamp(:startDate")
                   //.append(startDate)
                   .append(",'yyyy/MM/dd HH24:mi:ss') ");
                map.put("endDate", endDate+" 235959.999999999");
                map.put("startDate", startDate);
           }
        hql.append(" and settleOrderNum is null ")
           .append(" and jobId=:jobId");
        map.put("jobId", jobId); 
        return findByHql(hql.toString(),map);
    }

    @Override
    public void updateGainCalRecordsToCancelSettleOrder(String settleOrderNum) {
       HashMap<String, Object> map = new HashMap<String, Object>();
       map.put("settleOrderNum", settleOrderNum);
       updateBySql("update CPS_GAINS_CAL_RECORD  set SETTLE_ORDER_NUM=null where SETTLE_ORDER_NUM=:settleOrderNum",map);
    }

    @Override
    public List<CpsGainsCalRecord> getGainCalRecordBySettleOrderNum(String settleOrderNum) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("settleOrderNum", settleOrderNum);
        return findByHql("from CpsGainsCalRecord where settleOrderNum=:settleOrderNum",map);
    }

    @Override
    public CpsGainsCalRecord getGainCalRecordByRecordId(long recordId) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("recordId", recordId);
        List<CpsGainsCalRecord> records = findByHql("from CpsGainsCalRecord where sq=:recordId",map);
        return records==null||records.size()==0?null:records.get(0);
    }

	@Override
	public CpsGainsCalRecord getGainCalRecordByJobIdAndCalDate(String jobId,
			String CalDate) {
		HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("jobId", jobId);
        map.put("calDate", CalDate);
        List<CpsGainsCalRecord> records = findByHql("from CpsGainsCalRecord where jobId=:jobId and calDate=:calDate",map);
        return records==null||records.size()==0?null:records.get(0);
	}

   
}
